package com.zoranjoka.chordis.pages.add;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.zoranjoka.chordis.dao.AlbumDAO;
import com.zoranjoka.chordis.dao.ArtistDAO;
import com.zoranjoka.chordis.entities.Album;
import com.zoranjoka.chordis.entities.AlbumBean;
import com.zoranjoka.chordis.entities.Artist;
import com.zoranjoka.chordis.entities.ArtistBean;
import com.zoranjoka.chordis.entities.SongBean;
import com.zoranjoka.chordis.pages.Song;
import com.zoranjoka.chordis.pages.edit.EditAlbum;

public class AddSong {
	@Inject
	private ArtistDAO dataArtist;
	@Inject 
	private AlbumDAO dataAlbum;
	
	@InjectPage
	private Song Page;
	
	@Property
	@Persist
	private long albumId;
	@Property
	private String name;
	@Property
	private Artist artist;
	@Property
	@Persist
	private Album album;
	@Property 
	private SongBean song;
	@Property
	private List<SongBean> songs;
	
	void onActivate(long id){
		albumId=id;
//		artist = new ArtistBean();
//		album = new AlbumBean();	
//		song = new SongBean();
//		album = dataAlbum.findById(albumId);
//		artist = album.getArtist();
//		songs = album.getSongs();
//		song.setName("pesma1");
//		album.addSong(song);
//		song.setAlbum(album);
//		dataArtist.save(artist);
//		dataAlbum.save(album);
	}
	
	@CommitAfter
	Object onSubmit()
	{
		
		artist = new ArtistBean();
		album = new AlbumBean();	
		song = new SongBean();
		album = dataAlbum.findById(albumId);
		artist = album.getArtist();
		songs = album.getSongs();
		song.setName(name);
		album.addSong(song);
		song.setAlbum(album);
		dataArtist.save(artist);
		dataAlbum.save(album);
		
		
		return Page;
	}
	
}
